const entrys = require('./lib/entrys');
const rules = require('./lib/rules');
const plugins = require('./lib/plugins');
const resolve = require('./lib/resolve');
const { paths } = require('../utils');
const { webpackConfig, webpackDevConfig } = require('./lib/config');

module.exports = function(env, argv) {
  process.env.mode = process.env.mode || 'development';
  if (webpackDevConfig) {
    return webpackDevConfig;
  }
  const configPlugins = plugins({
    dll: webpackConfig.dll
  });

  return {
    entry: entrys(webpackConfig.entry),
    output: {
      path: paths.devPath,
      filename: 'js/[name].js',
      chunkFilename: 'js/chunks/[name].js',
      pathinfo: false,
      publicPath: paths.publicPath,
      sourceMapFilename: '[file].map',
      ...webpackConfig.output
    },
    devtool: webpackConfig.devtool,
    watchOptions: {
      aggregateTimeout: 0,
      ignored: [
        'node_modules',
        'asset-dev',
        'asset',
      ]
    },
    stats: {
      all: false,
    },
    performance: {
      hints: false,
      assetFilter(assetFilename) {
        return !(/^static\/.*\.js$/.test(assetFilename));
      }
    },
    module: {
      rules: rules({
        esLint: webpackConfig.esLint,
        lessLoaderOptions: webpackConfig.lessLoaderOptions
      })
    },
    resolve: resolve({
      extensions: webpackConfig.extensions,
      alias: webpackConfig.alias,
    }),
    optimization: {
      splitChunks: {
        chunks: 'async',
      }
    },
    plugins: [
      configPlugins.DllReferencePlugin,
      configPlugins.CopyWebpackPlugin,
      configPlugins.MiniCssExtractPlugin,
      configPlugins.FriendlyErrorsPlugin,
      // configPlugins.HtmlWebpackPlugin,
      ...webpackConfig.plugins,
      configPlugins.CleanWebpackPlugin,
    ],
  };
}


